class IdentitiesController < ApplicationController
  
  # GET /identities
  # GET /identities.xml
  def index
    @identities = Identity.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @identities.to_xml }
    end
  end

  # GET /identities/1
  # GET /identities/1.xml
  def show
    @identity = Identity.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @identity.to_xml }
    end
  end

  # GET /identities/new
  def new
    @identity = Identity.new
  end

  # GET /identities/1;edit
  def edit
    @identity = Identity.find(params[:id])
  end

  # POST /identities
  # POST /identities.xml
  def create
    @identity = current_user.identities.create(params[:identity])

    respond_to do |format|
      if @identity.save
        flash[:notice] = "The identity #{@identity.login}@#{@identity.service} was successfully created."
        format.html { redirect_to :controller => 'mmm', :action => 'index' }
        format.xml  { head :created, :location => identity_url(@identity) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @identity.errors.to_xml }
      end
    end
  end

  # PUT /identities/1
  # PUT /identities/1.xml
  def update
    @identity = Identity.find(params[:id])

    respond_to do |format|
      if @identity.update_attributes(params[:identity])
        flash[:notice] = 'Identity was successfully updated.'
        format.html { redirect_to identity_url(@identity) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @identity.errors.to_xml }
      end
    end
  end

  # DELETE /identities/1
  # DELETE /identities/1.xml
  def destroy
    @identity = Identity.find(params[:id])
    @identity.destroy

    flash[:notice] = "The identity #{@identity.login}@#{@identity.service} was sucessfully deleted" 

    respond_to do |format|
      format.html { redirect_to :controller => 'mmm', :action => 'index' }
      format.xml  { head :ok }
    end
  end
  
  def synchronize
    @identity = Identity.find(params[:id])
    @identity.synchronize
    
    flash[:notice] = "Sucked #{ @identity.contacts.size} contacts off #{ @identity.login}@#{@identity.service}"

    respond_to do |format|
      format.html { redirect_to :controller => 'mmm', :action => 'index' }
      format.xml  { head :ok }
    end
    
    
  end
  
  
end
